package com.orangelife.mobile.individual.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.login.adapter.SelectCommunityListViewAdapter;
import com.orangelife.mobile.main.activity.MainTablActivity;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择小区
 * 
 * @author yw
 * 
 */
@Deprecated
public class SwitchCommunityActivity extends OrangeLifeBaseActivity implements
		OnClickListener {
	/* 选择小区标题 */
	private TextView tvSelectCommunityTitle;
	/* 表示查找的图片 */
	private ImageView ivSearch;
	/* 查找的输入框 */
	private EditText etSearch;
	/* 小区列表 */
	private ListView lvCommunity;
	/** 选择小区的自定义适配器 */
	SelectCommunityListViewAdapter<Map<String, Object>> adapter;
	private List<Map<String, Object>> list;
	private Dialog dialog;
	private String cityName = "";
	private String cityName2 = "";
	private boolean isLocation = false;
	private String longitude;
	private String latitude;
	private String lon;
	private String lat;
	private String address;
	private Bundle bundle;
	private Map<String, Object> map;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		ExitApplication.getInstance().addActivity(this);
		int hui = 0;
		int shop = 0;
		bundle = new Bundle();
		setContentView(R.layout.activity_select_community);
		bundle = getIntent().getExtras();
		isLocation = bundle.getBoolean("isLocation", false);
		cityName2 = bundle.getString("cityName");
		longitude = bundle.getString("longitude");
		latitude = bundle.getString("latitude");
		getDataFromServer();
		initView();
		findView();

	}

	public void hideInput(View v) {
		InputMethodManager imm = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}

	private void initView() {
		tvSelectCommunityTitle = (TextView) findViewById(R.id.tv_title);
		ivSearch = (ImageView) findViewById(R.id.iv_search);
		etSearch = (EditText) findViewById(R.id.et_search_community);
		lvCommunity = (ListView) findViewById(R.id.lv_select_community);
	}

	private void findView() {
		tvSelectCommunityTitle.setText(getResources().getString(
				R.string.chose_community));
		ivSearch.setOnClickListener(this);
		list = new ArrayList<Map<String, Object>>();
		adapter = new SelectCommunityListViewAdapter<Map<String, Object>>(this,
				list);
		lvCommunity.setAdapter(adapter);
		lvCommunity.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String comId = list.get(position).get("commID").toString();
				String comName = list.get(position).get("comm").toString();
				String commImg = list.get(position).get("commImg").toString();
				sendNewCommInfo();
				IntentHelper.getInstances().getIntent(SwitchCommunityActivity.this,
						MainTablActivity.class);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_search:
			String desc = etSearch.getText().toString();
			dialog = DialogHelper.getInstance().createLoadingDialog(
					SwitchCommunityActivity.this, Constant.LOADING);
			dialog.show();
			sendRequestForGetComm(cityName, desc);
			break;

		default:
			break;
		}
	}

	private void getDataFromServer() {
		dialog = DialogHelper.getInstance().createLoadingDialog(SwitchCommunityActivity.this,
				Constant.LOADING);
		dialog.show();
		sendRequestForGetComm(cityName);
	}

	/**
	 * 搜索小区
	 * 
	 * @param cityName
	 *            城市名称 desc 搜索内容
	 * 
	 */
	private void sendRequestForGetComm(String cityName, String desc) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (isLocation) {
			map.put("{cityName}", cityName);
			map.put("longitude", lon);
			map.put("latitude", lat);
		} else {
			map.put("{cityName}", cityName2);
			map.put("longitude", longitude);
			map.put("latitude", latitude);
		}
		map.put("desc", desc);
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url", Constant.PATH_SELECT_COMM);
		rootMap.put("wat", 0);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}

	private void sendNewCommInfo() {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> rootmap = new HashMap<String, Object>();
		rootmap.put("map", map);
		rootmap.put("url", Constant.URL_CHANGE_COMMID);
		rootmap.put("wat", 3);
		new JSONRequest(JSONRequest.PUT, rootmap, handler, HttpUtil.ACTION_JSON);
	}

	/**
	 * 获取小区
	 * 
	 * @param cityName
	 * 
	 */
	private void sendRequestForGetComm(String cityName) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (isLocation) {
			map.put("{cityName}", cityName);
			map.put("longitude", lon);
			map.put("latitude", lat);
		} else {
			map.put("{cityName}", cityName2);
			map.put("longitude", longitude);
			map.put("latitude", latitude);
		}
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url", Constant.PATH_SELECT_COMM);
		rootMap.put("wat", 0);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}

	/**
	 * 接收处理线程返回的消息
	 */
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.WHAT_ERROR_HTTP:
				DialogHelper.closeDialog(dialog);
				ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
				break;
			case 0:
				if (msg.obj != null) {
					HashMap<String, Object> map = (HashMap<String, Object>) JSONHelper
							.jsonToMap(String.valueOf(msg.obj));
					if (map != null) {
						list = JsonReponseHandler.getListFromJson(
								String.valueOf(msg.obj), adapter, dialog);
						if (list != null) {
							adapter.setList(list);
							adapter.notifyDataSetChanged();
						}
						if (list.size() == 0) {
							ToastHelper
									.getInstance()
									.displayToastWithQuickClose(
											getResources()
													.getString(
															R.string.no_searsch_community_info));
						}
					}
				} else {
					ToastHelper.getInstance().displayToastWithQuickClose(
							getResources().getString(
									R.string.no_searsch_community_info));
				}
				break;
			case 1:
				if (msg.obj != null) {
					HashMap<String, Object> map = (HashMap<String, Object>) JSONHelper
							.jsonToMap(String.valueOf(msg.obj));
					if (map != null) {
						int is = Integer
								.parseInt(map.get("isCheck").toString());
					}
				} else {
					ToastHelper.getInstance().displayToastWithQuickClose(
							getResources().getString(
									R.string.failed_to_get_auth_info));
				}
				break;
			case 3:
				ToastHelper.getInstance()._toast(
						getResources().getString(
								R.string.change_community_successed));
				finish();
				break;
			case 1011:
				isLogin(dialog);
				break;
			}
		};
	};
}